package edu.hubu.talentmanagementsystem.config;

import edu.hubu.talentmanagementsystem.auth.JwtTokenAuthorizationTokenFilter;
import edu.hubu.talentmanagementsystem.auth.SecurityAccessDeniedHandler;
import edu.hubu.talentmanagementsystem.auth.SecurityAuthenticationEntryPoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @author moonlan
 * date 2021/2/9 下午2:33
 */
@SuppressWarnings(value = "unused")
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private final String[] permitAllUrls = {"/user/login", "/test", "/user/test-get-token", "/doc.html", "/service" +
            "-worker.js",
            "/swagger-resources/**", "/webjars/**", "/v2/api-docs", "/favicon.ico",
    "/staff/register","/company/register","/resource/testAdd","/resource/testGet"};

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers(permitAllUrls).permitAll()
                .anyRequest().authenticated()
                .and()
                .addFilterBefore(new JwtTokenAuthorizationTokenFilter(), UsernamePasswordAuthenticationFilter.class)
                .cors().and().csrf().disable()
                .exceptionHandling()
                .authenticationEntryPoint(new SecurityAuthenticationEntryPoint())
                .accessDeniedHandler(new SecurityAccessDeniedHandler());
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}
